class Btllib < Formula
  desc "Bioinformatics Technology Lab common code library"
  homepage "https://github.com/bcgsc/btllib"
  url "https://github.com/bcgsc/btllib/releases/download/v1.7.5/btllib-1.7.5.tar.gz"
  sha256 "118a9f8d6445a618178bfbec40d121bbe03014e767261522148f642686090c76"
  license "GPL-3.0-or-later"

  bottle do
    sha256 cellar: :any,                 arm64_tahoe:   "f28456234329c7647f0f2cc58540744e0f9790b626b77d62cc4e1fc15811857f"
    sha256 cellar: :any,                 arm64_sequoia: "62a4e2af4d7fc2c9d4a49e354943f4581629e399f78b6f31ad41458859651179"
    sha256 cellar: :any,                 arm64_sonoma:  "023e2d26013d71ba1f754c22ddca0993f33801e70d17d542a3776b144b0cae6e"
    sha256 cellar: :any,                 sonoma:        "e5c99cf0f8b060c9fed4bf52fe89bf90fd5e1535571fe59b67b0a80323afe58d"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "ad898baf48d8ac49e1d40209b1a1e5a5f47cf3a79fbffb6975e55dcd77194e90"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "3473b3fa6fc735ad5f8262edf31a5decf2d998cf046880420dec5dc97679bac0"
  end

  depends_on "cmake" => :build
  depends_on "meson" => :build
  depends_on "ninja" => :build
  depends_on "python@3.13" => :build # cxx file generated by swig is not compatible with 3.14

  on_macos do
    depends_on "libomp"
  end

  # Apply FreeBSD patch to fix build with newer Clang, https://github.com/simongog/sdsl-lite/issues/462
  patch :p0 do
    url "https://raw.githubusercontent.com/freebsd/freebsd-ports/af74f60a871e4a5aa7aea787fc235a2cb760e764/devel/sdsl-lite/files/patch-include_sdsl_louds__tree.hpp"
    sha256 "84aef67058947044c40032ac39d9b6d1b8a285c581f660d565cd23aaa4beade7"
    directory "subprojects/sdsl-lite"
  end

  def install
    # Workaround for CMake 4 compatibility
    ENV["CMAKE_POLICY_VERSION_MINIMUM"] = "3.5"

    args = %w[-Db_ndebug=true -Db_coverage=false]
    system "meson", "setup", "build", *args, *std_meson_args
    system "meson", "compile", "-C", "build", "--verbose"
    system "meson", "install", "-C", "build"
  end

  test do
    (testpath/"test.cpp").write <<~CPP
      #include "btllib/seq.hpp"

      #include <string>

      int main()
      {
        std::string seq = "ACGTACACTGGACTGAGTCT";
        std::string rc = "AGACTCAGTCCAGTGTACGT";
        if (btllib::get_reverse_complement(seq) != rc) {
          return 1;
        }
        return 0;
      }
    CPP
    system ENV.cxx, "test.cpp", "-std=c++11", "-I#{include}", "-L#{lib}", "-lbtllib", "-o", "test"
    system "./test"
  end
end
